package com.stack.knowyoubase.util;

import com.stack.knowyoubase.constant.GlobalConst;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;


/**
 * 分页工具类
 *
 * @author stack
 */
@Component
@Scope("singleton")
public class PageUtil {

    /**
     * @param page  页码
     * @param limit 每页的数据量
     * @return 查询数据库分页的参数
     */
    public HashMap<String, Integer> pageCreate(Integer page, Integer limit) {
        int pageNum;
        int limitNum;
        if (page == null) {
            pageNum = GlobalConst.PAGE_NUM;
        } else {
            pageNum = page - 1;
        }
        if (limit == null) {
            limitNum = GlobalConst.LIMIT_NUM;
            pageNum = pageNum * limitNum;
        } else {
            pageNum = pageNum * limit;
            limitNum = limit;
        }
        HashMap<String, Integer> pageHelp = new HashMap<>(2);
        pageHelp.put("page", pageNum);
        pageHelp.put("limit", limitNum);
        return pageHelp;
    }
}
